package com.lzh.others.leetcode;

/**
 *
 * 每个元音包含偶数次的最长子字符串
 * 给你一个字符串 s ，
 * 请你返回满足以下条件的最长子字符串的长度：
 * 每个元音字母，即 'a'，'e'，'i'，'o'，'u' ，在子字符串中都恰好出现了偶数次。
 * <p>
 * 前提：
 * s字符串 只包含小写英文字母
 */
public class LeetCode1371 {

    //暴力解法：顺序遍历所有字串，取符合条件最长的子串

    static final char A = 'a';
    static final char E = 'e';
    static final char I = 'i';
    static final char O = 'o';
    static final char U = 'u';

    /**
     * 判断字符串中元音字母出现的次数是否是偶数
     *
     * @param s
     * @return true偶数;false奇数
     */
    public static boolean allAreEvenNum(String s) {
        int countA = 0;
        int countE = 0;
        int countI = 0;
        int countO = 0;
        int countU = 0;
        int length = s.length();
        for (int i = 0; i < length; i++) {
            char temp = s.charAt(i);
            if (temp == A) {
                countA++;
            }
            if (temp == E) {
                countE++;
            }
            if (temp == I) {
                countI++;
            }
            if (temp == O) {
                countO++;
            }
            if (temp == U) {
                countU++;
            }

        }
        if (countA % 2 != 0 || countE % 2 != 0 || countI % 2 != 0 || countO % 2 != 0 || countU % 2 != 0) {
            return false;
        }

        return true;
    }

    public static int findTheLongestSubstring(String s) {
        int result = 0;
        int length = s.length();
        for (int i = 0; i < length; i++) {
            StringBuilder tempStr = new StringBuilder();
            tempStr.append(s.charAt(i));
            for (int j = i + 1; j < length; j++) {
                tempStr.append(s.charAt(j));
                if (allAreEvenNum(tempStr.toString())) {
                    result = Math.max(tempStr.length(), result);
                }
            }
            if (i + 1 == length && result == 0 && allAreEvenNum(tempStr.toString())) {
                result = 1;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        long l = System.currentTimeMillis();
        String s =
        "lcwbgnkxnooovtxtuuolbgvieqyxdzyngkdaxuhgwzmgjjvylhavhtijfzvwdalxewsympoyvdhtlbchfgnexmmslcbrozsljwlxzkladmjibvyqpibpkwnxosrqhfqbmigkgxdtfnnsitxecihwisfonaejqeenboqxpqxbmdwzxjnodurnznaitbjikqaerbgkxdfjxnqyecqpqcnluezomtkvjprcqeaucgttwlpqeyfwenybcluxwzjvixlljnmpolktwnezewpiuwinsptbjqzplpeoeuwpehkycvrsslfznunjihtauplcipxmobnipqekegxmddzkepuqzoqepebgwmkuxipjbncgaskmzjhjnlokvxidvlbjgxsdtxiaikdmojrilepbdmokaouhekegpfjlhuywvgtbtozivvooplnnchlbkdvmrwwpkegczippokorcptixrudwszencxemdnxrgearvtddcukdvsrulnvmcnuojxynomtrteotpmdkiueivbdyfxvbakkbfnotanrsmdvmbaehqpumkejtessereyeshxgagprozlpunqanhwmitayqkdzqyhnieqosyrlidxxxcwlmolnvqopmrabpkyhdalcwfwpdheoxcfuiypwlfhbjuorrotjhajpeggfjcifmuzyszjxyfomvczltlhazrtfotejmekkiaegiguabubojhqyudxctegfanffokbotqlwjsertqbhdophqkdvjeiunidaalqfbkfbbaihaxjpxxtupkscgflwxczlelgomoltschhdhcxjqlcdstlhrjyujzdfwngoeygcncmeshjitqxspmddvlopvmgskeuikugtbvmikzdueglpuksbbwoeopqygziitejdkhkkgydngnyeqrcmamemltyqmtexvcyvqhygppmywkiraqyqwxxiamxqmeftidndwpmbpcschkmtvvklwvyihneyhvmtxbhgseskfdygnbqkrywllvwnhlymchoelulcoqznmyicooveiwwazqlnhlwozkoqyaezwyhzwdhilhsfollwbwvfmrllvrsnjqiocbdrixjzgnrszghicnmvqjjtpcnhwgpdqlsyrcbckhocgboecvpczbepewpnpjatirudtcurrtlbhrvreaymbekvmrzctyojkgnpcmyizauvudjxhssmamcuschcnqorjqybepptlerdgirzapuvsoeekmoitoszlwmyvtscagrhhtvlnrydcayhukhxgqfrcqfbjuiswvklnlygatspefmafailygnvgzhnvsgnsmbfsaugdbjermtlfsyvffqepucofzyjilohswbxhugzzihzxwnxwiwhlzpnzgoqvqzkbhhrendrcynnqmtodzgbmgrwgnqxqwtqtpyludmuvyjqzlkfnvzrhgchcivnxhpuibmlmongbjheekvvxrvjtsonvjlxkihomxkbsygzyvdvuehlewqoymiankekcgmgmtqrcvuogdezmdmyqsxzyhoyprcfyalbsoeypmgxgtbkgpixfrpqxqnbegajnwcfuamjqejefjqhzzvjzotgqgwbgtmklvmybvmcfpeljfbwpollltqxrgyrehnzvchijrokuoapwpeanmfjrwmgsvmzxwahupklojcbqkfeblphtuwbbapooynhmfdhjjhadgnvjpyizjduoeikzjsrkbjgioqxqhyakodcnlibewwuqjlzkmfybokfthpftnrmdjyctofsnpuexcarwrpstrehbrzjnzvsoislrkqlsbfnhpulvixxyhctijqybjppwfcvsqrgbssqkjcrthsufevxnutkuwgtyovfaygbacmzlngfanbhbmdllrtemqudxkpuaohmrcntsmalqjhmpmlbbntnsfwekgepnkyyalllxczymewubebxjikbceehkgyfognlejzsfoasfxudbxenvjqygbctqyqrwnxiwprooustcskdjhybhdgfuymymueshrugpwsuevxuydmolvatzpupybhtjfqtpcxkciwzqjverubkceqwefvyatrabcyrlpbvquvrcoosvailoclcffysasmgsvhepdlqvptdxtrvfqjdjctdskrosxsjojyqozzrosejsicvjbsmwepsuyxwkvidsjnhhmrscmveibviyhkcimdehpkonycqsobngmssvyittyeembauboalqokzfwvgxotgvgmkywoiifjvcyttrcqtpssrhdpdoqrfdwukqwzmshqdzohdycpisqjaktzvzmhneqsocpinxzrsrcwdmdimbcrlcmvppxvuufmbimvmmoawvhrecpmgdywwogmumnaziaybpxneytfxgqmjcrbnweofjbujsjbtqkjbpcfxuyvytmwupiozanbjyblgkfhecbdejkpsstpamsrmhxqerydnrmhratsrknzmruvgenfdsdxjflppyqfsiuwhicwqkhuztrhiyfuebolethjxbnopxbfrmzvvpuxkjiflvqxxlcynhwldlhwclvrvvvjbslqamvdaasfodglcqemfotdqiwpzflveduecumffhzjdbtgatwgazuowgtmjekltmdzttgkanoxgfaqiohzccgtxehzhwrlqqasqgobdfnevzpqhhblfzrvfnrhilyyuztrgjshdrbkjrfxenkrjtayurhakpbgmdowuzmprrgyyodvugyyufuitwiaczvvmazgqkiusgypreobmqmmbubjlhxgorppbyrgdkwjnxzhwxfisivrsldvwlnqnophdfqtotobdcmmyiguvpsvvltmrqzdsahpmiticfmdzb";
        int nums = findTheLongestSubstring(s);
        System.out.println("result = " + nums);
        System.out.println("cost = " + (System.currentTimeMillis() - l) + "ms");
    }
}
